package com.zw.yzk.kabu.management;


import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.view.widget.LoadingDialog;

/**
 * Created by zhanwei on 2017/2/24.
 */

public class LoadingDialogManager {
    private static LoadingDialogManager instance;
    private LoadingDialog loadingDialog;

    private LoadingDialogManager() {
    }

    public static LoadingDialogManager getInstance() {
        if (instance == null) {
            synchronized (LoadingDialogManager.class) {
                instance = new LoadingDialogManager();
            }
        }
        return instance;
    }

    public Dialog showLoadingDialog(AppCompatActivity activity) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog();
        }
        //显示对话框
        if (!activity.isFinishing() && !loadingDialog.isAdded() && !loadingDialog.isVisible() && !loadingDialog.isRemoving()) {
            activity.getSupportFragmentManager().beginTransaction()
                    .add(loadingDialog, loadingDialog.getClass().getSimpleName())
                    .commitAllowingStateLoss();
            activity.getSupportFragmentManager().executePendingTransactions();
        }
        return loadingDialog.getDialog();
    }

    public void dismissLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.disMissDialog();
        }
    }
}
